import is.glif.fenius.ws.simple.SimpleFeniusService;
import is.glif.fenius.ws.simple.Simple;

import is.glif.fenius.ws.simple.ReservationParams;
import is.glif.fenius.ws.simple.CreateReq;
import is.glif.fenius.ws.simple.CreateResp;
import is.glif.fenius.ws.simple.ListReq;
import is.glif.fenius.ws.simple.ListResp;

import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.DatatypeConfigurationException;

import java.util.List;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.GregorianCalendar;

public class SimpleTest {
    static boolean debug = true;
    private SimpleFeniusService tl = null;

    public SimpleTest(SimpleFeniusService tl) 
    {
	this.tl = tl;
    }

	
    public boolean create(ReservationParams p) 
    {
	if (tl == null) return false;

	try {
	    if (debug) printParams("Create request", p);
	    CreateReq req = new CreateReq();
	    req.setResvParams(p);

	    CreateResp res = tl.create(req);

	    ReservationParams rp = res.getResvParams();
	    if (debug) printParams("Create response", rp);
	} catch (Exception e) {
	    e.printStackTrace();
	    return false;
	}
	return true;
    }

    public boolean list() 
    {
	if (tl == null) return false;

	try {
	    if (debug) printParams("List request", null);
	    ListReq req = new ListReq();

	    ListResp res = tl.list(req);

	    List<ReservationParams> rps = res.getReservations();
	    if (rps == null) return false;

	    int size = rps.size();
	    ReservationParams[] rp = new ReservationParams[size];
	    ReservationParams[] rpp = rps.toArray(rp);
	    for (int i = 0; i < size; i++) {
		if (debug) printParams("Create response", rpp[i]);
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    return false;
	}
	return true;
    }

    private void printParams(String m, ReservationParams p)
    {
	String s = m + ":\n";
	if (p != null) {
	    m += "\t" + p.getReservationId() 
		+ "::" + p.getSrcEndpoint() 
		+ "->" + p.getDstEndpoint()  
		+ ":" + p.getStatus();
	    
	    XMLGregorianCalendar c = p.getActivationTime();
	    m += ", Time=" + c.getYear() + "/" + c.getMonth() 
		+ "/" + c.getDay();
	    m += " " + c.getHour() + ":" + c.getMinute() + ":" + c.getSecond();
	    m += ", Duration=" + p.getDuration() + ", BW=" + p.getBandwidth();
	}
	System.out.println(m);
    }

    public boolean createTest() 
    {
	ReservationParams p = new ReservationParams();

	Calendar startTime = Calendar.getInstance();
	startTime.setTime(new Date());
	startTime.set(Calendar.MILLISECOND, 0);
	startTime.add(Calendar.MINUTE, 1);

	Date date = startTime.getTime();
	GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
	gc.setTime(date);
	DatatypeFactory df = null;
	try {
	     df = DatatypeFactory.newInstance();
	} catch (javax.xml.datatype.DatatypeConfigurationException e) {
	    e.printStackTrace();
	    return false;
	}
	XMLGregorianCalendar xgc = df.newXMLGregorianCalendar(gc);
;
	p.setReservationId("TEST");
	p.setSrcEndpoint("IXA1");
	p.setDstEndpoint("IXB2");
	p.setBandwidth(100);
	p.setActivationTime(xgc);
	p.setDuration(120);
	p.setStatus("INIT");

	return create(p);
    }

    public boolean listTest() {
	return list();
    }
    
    public static void main(String[] args) {
	String timezone = "UTC";
	// String timezone = "Asia/Tokyo";
	TimeZone.setDefault(TimeZone.getTimeZone(timezone));

	Simple service = new Simple();
        SimpleFeniusService port = service.getSimpleFeniusServicePort();
	SimpleTest t = new SimpleTest(port);

	if (args != null && args.length != 0 &&
	    args[0].equals("create")) {
	    t.createTest();
	} else if (args != null && args.length != 0 &&
		   args[0].equals("list")) {
	    t.listTest();
	} else {
	    String s = "Test create | list\n" +
		"\t create: create reservation\n" +
		"\t list:   list reservation\n";
	    System.err.println(s);
	}
    }
}